package yanyan.com.tochar;

import android.Manifest;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.hjq.permissions.Permission;
import com.kuaiyou.loader.AdViewSpreadManager;
import com.kuaiyou.loader.InitSDKManager;
import com.kuaiyou.loader.loaderInterface.AdViewSpreadListener;
import com.umeng.analytics.MobclickAgent;
import com.umeng.commonsdk.UMConfigure;
import com.umeng.message.IUmengRegisterCallback;
import com.umeng.message.PushAgent;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;
import yanyan.com.tochar.beans.AllInfo;
import yanyan.com.tochar.beans.AppInfo;
import yanyan.com.tochar.beans.BannerInfo;
import yanyan.com.tochar.beans.WebAppInfo;
import yanyan.com.tochar.utils.CoreUtil;
import yanyan.com.tochar.utils.HttpUtil;
import yanyan.com.tochar.utils.PermissionsUtil;
import yanyan.com.tochar.utils.StringUtil;
import yanyan.com.tochar.utils.ToastUtil;

public class StartActivity extends BaseActivity implements AdViewSpreadListener {
    public static String APPID = "SDK201914230212238s0yhn9cf36d2cd";
//    private TextView textView;
    public String[] permissions = null;
    private AdViewSpreadManager adSpreadBIDView = null;
    @Override
    protected void onCreate( Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_start);
//        textView=findViewById(R.id.start_text);
        /**
         * 友盟
         */
        UMConfigure.init(this,"5c6eba17b465f53aeb000b8b","Umeng",UMConfigure.DEVICE_TYPE_PHONE,"0f0ff36f01016a6a58dc26ab95c6435b");
        PushAgent.getInstance(this).onAppStart();
        message();
        this.onResume();
        this.onPause();

        getAppInfo();
        CoreUtil.goMainFlag=0;
        //广告
        adView();
//        Handler handler = new Handler();
//        handler.postDelayed(new gotoPage(), 3000);00

    }


    private void message(){
        //获取消息推送代理示例
        PushAgent mPushAgent = PushAgent.getInstance(this);
//注册推送服务，每次调用register方法都会回调该接口
        mPushAgent.register(new IUmengRegisterCallback() {
            @Override
            public void onSuccess(String deviceToken) {
            }
            @Override
            public void onFailure(String s, String s1) {
            }
        });
    }

    private void adView(){

        InitSDKManager.getInstance().init(this, APPID);
        //下载类广告默认弹出二次确认框，如需关闭提示请设置如下；设置后对全部广告生效
        InitSDKManager.setDownloadNotificationEnable(false);
        //在调用SDK之前，如果您的App的targetSDKVersion >= 23，那么一定要把"READ_PHONE_STATE"、"WRITE_EXTERNAL_STORAGE"、"ACCESS_FINE_LOCATION"这几个权限申请到
       permissions = new String[]{Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.READ_PHONE_STATE};
       // permissions = new String[]{Permission.READ_PHONE_STATE,Permission.WRITE_EXTERNAL_STORAGE,Permission.ACCESS_FINE_LOCATION};
        if (Build.VERSION.SDK_INT >= 23) {
            requestRunTimePermission(permissions, new PermissionListener() {

                @Override
                public void onGranted(List<String> grantedPermission) {
                    this.onGranted();
                }

                @Override
                public void onGranted() {
                    requestSpreadAd();
                }

                @Override
                public void onDenied(List<String> deniedPermission) {
                    requestSpreadAd();
                    Toast.makeText(StartActivity.this,  "权限被拒绝了", Toast.LENGTH_SHORT).show();
                }
            });
        } else {
             requestSpreadAd();
        }
    }


    private void requestSpreadAd() {
        adSpreadBIDView = new AdViewSpreadManager(this, APPID,"POSID5ofy2mjwved6",
                (RelativeLayout) findViewById(R.id.start_spreadlayout));
//        adSpreadBIDView = new AdViewSpreadManager(this,APPID,
//                (RelativeLayout) findViewById(R.id.start_spreadlayout));
        adSpreadBIDView.setBackgroundColor(Color.WHITE);
        adSpreadBIDView.setSpreadNotifyType(AdViewSpreadManager.NOTIFY_COUNTER_NUM);
        adSpreadBIDView.setOnAdViewListener(this);
    }

    /**
     * 统计人数
     */
    @Override
    public void onResume() {
        super.onResume();
        MobclickAgent.onResume(this);
    }

    /**
     * 统计时长
     */
    @Override
    public void onPause() {
        super.onPause();
        MobclickAgent.onPause(this);
    }


    /**
     * 获取
     */
    public void getAppInfo(){
        Call msg = HttpUtil.getMsg("http://39.106.162.25:8082/appInfo/getAll");
        msg.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {

            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String res=response.body().string();
                try{
                    System.out.println(res);
                    AllInfo allInfo = JSON.parseObject(res, AllInfo.class);
                    if(allInfo!=null){
                        CoreUtil.appInfo=allInfo.getAppInfo();
                        CoreUtil.bannerInfo=allInfo.getBannerInfos();
                        CoreUtil.webAppInfos=allInfo.getWebInfos();
                        CoreUtil.flag=true;
                    }

                }catch (Exception e){
                  System.out.print(e.getMessage());
                }

            }
        });


    }




//    class gotoPage implements Runnable{
//        @Override
//        public void run() {
//            Intent intent = new Intent();
//            intent.setClass(StartActivity.this, MainActivity.class);
//            startActivity(intent);
//            StartActivity.this.finish();
//        }
//    }
//
//
//    private void ztl()
//    {
//        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
//            // 设置状态栏透明
//            this.getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
//            // 设置根布局的参数
//            ViewGroup rootView = (ViewGroup) ((ViewGroup) this.findViewById(android.R.id.content)).getChildAt(0);
//            rootView.setFitsSystemWindows(true);
//            rootView.setClipToPadding(true);
//        }
//
//    }


    @Override
    public void onAdClicked() {
        System.out.println("1");
    }

    @Override
    public void onAdDisplayed() {
//        textView.setVisibility(View.GONE);
        System.out.println("2");
    }

    @Override
    public void onAdReceived() {

        System.out.println("3");
    }

    @Override
    public void onAdFailedReceived(String s) {
        if(CoreUtil.goMainFlag==0){
            CoreUtil.goMainFlag++;
            gotoMain();
        }
    }

    @Override
    public void onAdClosed() {
        if(CoreUtil.goMainFlag==0){
            CoreUtil.goMainFlag++;
            gotoMain();
        }
    }

    @Override
    public void onAdSpreadPrepareClosed() {
        if(CoreUtil.goMainFlag==0){
            CoreUtil.goMainFlag++;
            gotoMain();
        }
    }

    @Override
    public void onAdClosedByUser() {
        if(CoreUtil.goMainFlag==0){
            CoreUtil.goMainFlag++;
            gotoMain();
        }
    }

    @Override
    public void onRenderSuccess() {

    }

    @Override
    public void onAdNotifyCustomCallback(int i, int i1) {

    }


    private void gotoMain(){
        Intent intent = new Intent();
        intent.setClass(StartActivity.this, MainActivity.class);
        startActivity(intent);
        StartActivity.this.finish();
    }


}

